package logic

import (
	"context"
	"fmt"
	"wechat/models/mysql"

	"wechat/userrpc/internal/svc"
	"wechat/userrpc/userrpc"

	"github.com/zeromicro/go-zero/core/logx"
)

type RecordListLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewRecordListLogic(ctx context.Context, svcCtx *svc.ServiceContext) *RecordListLogic {
	return &RecordListLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

// 申请列表
func (l *RecordListLogic) RecordList(in *userrpc.RecordListRequest) (*userrpc.RecordListResponse, error) {
	// todo: add your logic here and delete this line

	var user mysql.User

	mysql.DB.Where("id = ?", in.UserId).Find(&user)

	if user.ID == 0 {
		return nil, fmt.Errorf("当前用户不存在")
	}

	var RecordUsers []mysql.RecordUser

	mysql.DB.Debug().Raw("select apply_records.user_id,apply_records.context, users.head_image,apply_records.id,users.name,CASE apply_records.STATUS  WHEN 0 THEN '申请中'  WHEN 1 THEN '已同意' ELSE '已拒绝'  END status from apply_records left join users on apply_records.user_id = users.id where apply_records.friend_id = ?", in.UserId).Scan(&RecordUsers)
	fmt.Println(RecordUsers)

	var recordLists []*userrpc.RecordList

	for _, record := range RecordUsers {

		list := userrpc.RecordList{
			UserId:    in.UserId,
			Context:   record.Context,
			HeadImage: record.HeadImage,
			Id:        record.Id,
			Name:      record.Name,
			Status:    record.Status,
		}

		recordLists = append(recordLists, &list)
	}

	return &userrpc.RecordListResponse{List: recordLists}, nil
}
